<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CateController extends Controller
{
    /**
     * 分类管理
     */
    public function index(Request $request)
    {
        //获取一级类别信息
        $firstCateList = Category::getCateList();

        return view("admin/cate/index", ["firstCateList" => $firstCateList]);
    }

    /**
     * 获取列表
     */
    public function getList(Request $request)
    {
        $pid = $request->get('pid') ? $request->get('pid') : 0;
        $page = $request->get('page') ? $request->get('page') : 1;
        $limit = $request->get('limit') ? $request->get('limit') : 1;
        //获取一级类别信息
        $total = Category::where(['status' => 1, "pid" => $pid])->count();
        $list = Category::where(['status' => 1, "pid" => $pid])
            ->orderBy('sort', 'asc')
            ->offset(($page - 1) * $limit)
            ->limit($limit)
            ->get()
            ->toArray();
        foreach ($list as &$cate) {
            if ($cate['pid'] == 0) {
                $cate['pidname'] = '一级分类';
            } else {
                $info = Category::getInfo($cate['pid']);
                $cate['pidname'] = $info['name'];
            }
            $cate['status'] = $cate['status'] == 1 ? '有效' : "无效";
        }
        return [
            'code' => 0,
            'msg' => 'success',
            'count' => $total,
            'data' => $list,
        ];
    }

    public function create(Request $request)
    {
        if ($request->method() == 'POST') {
            $data["created_at"] = date('Y-m-d H:i:s', time());
            $data["updated_at"] = date('Y-m-d H:i:s', time());
            $data["sort"] = 0;
            $result = DB::table("category")->insert(array_merge($request->post(),$data));
            if($result === false){
                return [
                    'code' => 0,
                    'msg' => 'false',
                ];
            }else{
                return [
                    'code' => 1,
                    'msg' => 'success',
                ];
            }
        } else {
            $firstCateList = Category::getCateList();
            return view("admin/cate/addform", ['firstCateList' => $firstCateList]);
        }
    }

    public function del(Request $request)
    {
        if ($request->method() == 'POST') {
            $result = DB::table("category")->whereIn('id', $request->post())->update(['status'=>0]);
            if($result){
                return [
                    'code' => 1,
                    'msg' => 'success',
                ];
            }
        }
        return [
            'code' => 0,
            'msg' => 'false',
        ];
    }
    public function edit(Request $request)
    {
        if ($request->method() == 'POST') {
            $id = $request->get('id');
            $result = DB::table("category")->where('id',$id)->update($request->post());
            if($result){
                return [
                    'code' => 1,
                    'msg' => 'success',
                ];
            }
        }else{
            $firstCateList = Category::getCateList();
            $cateInfo = Category::getInfo($request->get('id'));
            return view("admin/cate/editform", ['firstCateList' => $firstCateList,'info'=>$cateInfo]);
        }
    }


}
